import 'dart:io';
import 'package:device_info_plus/device_info_plus.dart';
import 'package:package_info_plus/package_info_plus.dart';
import 'package:my_shop/util/constant.dart';
import 'package:sp_util/sp_util.dart';
import 'package:uuid/uuid.dart';

class DeviceInfoUtil {
  static final DeviceInfoUtil _instance = DeviceInfoUtil._internal();
  static late String version;
  static late String deviceId;
  static Map<String, dynamic> deviceInfo = {};

  factory DeviceInfoUtil() => _instance;

  DeviceInfoUtil._internal();

  static Future<void> initDeviceInfo() async {
    final packageInfo = await PackageInfo.fromPlatform();
    version = packageInfo.version;

    final deviceInfoPlugin = DeviceInfoPlugin();
    final info = await deviceInfoPlugin.deviceInfo;
    deviceInfo = info.data;
    deviceInfo['appVersion'] = version;
    deviceInfo['systemName'] = Platform.operatingSystem;
    deviceInfo['systemVersion'] = Platform.operatingSystemVersion;

    if (Platform.isAndroid) {
      deviceInfo.remove('systemFeatures');
      deviceId = deviceInfo['androidId'] ?? _getDeviceId();
    } else if (Platform.isIOS) {
      final ustNameMap = deviceInfo['utsname'];
      final nodename = ustNameMap['nodename'];
      deviceInfo['name'] = nodename;
      deviceId = deviceInfo['identifierForVendor'] ?? _getDeviceId();
    }

    deviceInfo['deviceId'] = deviceId;
  }

  static String _getDeviceId() {
    var deviceId = SpUtil.getString(Constant.deviceId);
    if (deviceId == null || deviceId.isEmpty) {
      deviceId = const Uuid().v4();
      SpUtil.putString(Constant.deviceId, deviceId);
    }
    return deviceId;
  }

  /// todo 内容如下并没有deviceId
/// deviceInfo = {CastMap} {product: PD2309, supportedAbis: [arm64-v8a], serialNumber: unknown, displayMetrics: {xDpi: 452.8009948730469, widthPx: 1260.0,
//  _source = {_Map} size = 22
//   0 = {map entry} "product" -> "PD2309"
//   1 = {map entry} "supportedAbis" -> [_List]
//   2 = {map entry} "serialNumber" -> "unknown"
//   3 = {map entry} "displayMetrics" -> [_Map]
//   4 = {map entry} "supported32BitAbis" -> [_List]
//   5 = {map entry} "display" -> "PD2309_A_15.1.17.5.W10.V000L1"
//   6 = {map entry} "type" -> "user"
//   7 = {map entry} "isPhysicalDevice" -> true
//   8 = {map entry} "version" -> [_Map]
//   9 = {map entry} "systemFeatures" -> [_List]
//   10 = {map entry} "manufacturer" -> "vivo"
//   11 = {map entry} "tags" -> "release-keys"
//   12 = {map entry} "supported64BitAbis" -> [_List]
//   13 = {map entry} "bootloader" -> "unknown"
//   14 = {map entry} "fingerprint" -> "vivo/PD2309/PD2309:15/AP3A.240905.015.A1/compiler04171954:user/release-keys"
//   15 = {map entry} "host" -> "comdg01150026"
//   16 = {map entry} "model" -> "V2309A"
//   17 = {map entry} "id" -> "AP3A.240905.015.A1"
//   18 = {map entry} "brand" -> "vivo"
//   19 = {map entry} "device" -> "PD2309"
//   20 = {map entry} "board" -> "k6989v1_64"
//   21 = {map entry} "hardware" -> "mt6989"
}
